package com.ukao.cashregister.ui.setting;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.UpgradeInfo;
import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseFragment;
import com.ukao.cashregister.utils.AppManager;
import com.ukao.cashregister.utils.AppUtils;
import com.ukao.cashregister.utils.CheckUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 版本升级
 */
public class VersionFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.quit)
    TextView quit;
    Unbinder unbinder;
    @BindView(R.id.versions)
    TextView versions;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.fileSize)
    TextView fileSize;
    @BindView(R.id.newFeature)
    TextView newFeature;
    @BindView(R.id.ver_titile)
    TextView verTitile;
    @BindView(R.id.ver_titile_latout)
    LinearLayout verTitileLatout;


    private String mParam1;
    private String mParam2;
    private  int  i;

    public static VersionFragment newInstance(String param1, String param2) {
        VersionFragment fragment = new VersionFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_version, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();


        return view;
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!CheckUtils.isMIUIDevices()){
            loadUpgradeInfo();
        }

    }

    @Override
    protected void initView() {
        i=0;
        title.setText(resources.getString(R.string.app_name)+ AppUtils.getVersionName(_mActivity)+"已是最新版本");


    }

    private void loadUpgradeInfo() {
        verTitileLatout.setVisibility(View.GONE);
        title.setVisibility(View.VISIBLE);
        versions.setVisibility(View.GONE);

            /***** 获取升级信息 *****/
            UpgradeInfo upgradeInfo = Beta.getUpgradeInfo();
          if (upgradeInfo!=null&&AppUtils.getVersionCode(_mActivity)<upgradeInfo.versionCode){
                int size = (int) (upgradeInfo.fileSize / 100000);
                double mFilasizi = (double) size / 10;
                fileSize.setText(mFilasizi+"MB");
                newFeature.setText(upgradeInfo.newFeature);
                verTitile.setText(upgradeInfo.title);
                if (mFilasizi>0){
                    versions.setVisibility(View.VISIBLE);
                    verTitileLatout.setVisibility(View.VISIBLE);
                    title.setVisibility(View.GONE);
                }

            }





    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.versions, R.id.quit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.versions:
                Beta.checkUpgrade();

                break;
            case R.id.quit:
            i++;
                if (i>5){
                    AppManager.getAppManager().AppExit(_mActivity);
                }

                break;
        }
    }
}
